﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Product_P_Details: System.Web.UI.Page,ICallbackEventHandler
{
    public string kc = "";
    private string _productid;
    private string _color;
    private string _zk;
    private string _productname; //商品名称
    private string _brand;       //品牌
    private string _productno;  //商品编号
    private string _fabrics;    //面料
    private string _weight;     //重量
    private string _style;      //风格
    private string _stock;      //库存
    private string _price;        //吊牌价

    private string _colorlist;  //颜色列表
    private string _sizelist;   //尺寸列表
    private string _selectcolor; //选中颜色
    private string _partimglist;//局部图片列表

    private string _bigsrc;     //大图路径
    private string _note;       //描述
    private string _webTitle; //页面标题
    #region 
    public string ProductID
    {
        get { return _productid; }
        set { _productid = value; }
    }
    public string Color
    {
        get { return _color; }
        set { _color = value; }
    }
    public string ZK
    {
        get { return _zk; }
        set { _zk = value; }
    }
    public string ProductName
    {
        get { return _productname; }
        set { _productname = value; }
    }
    public string Brand
    {
        get { return _brand; }
        set { _brand = value; }
    }
    public string ProductNo
    {
        get { return _productno; }
        set { _productno = value; }
    }
    public string Fabrics
    {
        get { return _fabrics; }
        set { _fabrics = value; }
    }
    public string Weight
    {
        get { return _weight; }
        set { _weight = value; }
    }
    public string Style
    {
        get { return _style; }
        set { _style = value; }
    }
    public string Stock
    {
        get { return _stock; }
        set { _stock = value; }
    }
    public string Price
    {
        get { return _price; }
        set { _price = value; }
    }
    public string Colorlist
    {
        get { return _colorlist; }
        set { _colorlist = value; }
    }
    public string Sizelist
    {
        get { return _sizelist; }
        set { _sizelist = value; }
    }
    public string SelectColor
    {
        get { return _selectcolor; }
        set { _selectcolor = value; }
    }
    public string Partimglist
    {
        get { return _partimglist; }
        set { _partimglist = value; }
    }
    public string Bigsrc
    {
        get { return _bigsrc; }
        set { _bigsrc = value; }
    }
    public string Note
    {
        get { return _note; }
        set { _note = value; }
    }
    public string WebTitle
    {
        get { return _webTitle; }
        set { _webTitle = value; }
    }
    #endregion
    private string DPJ = BasePage.GetSJ(); //从数据库中读取 吊牌价

    private string CaiZhi = "FJSX3"; //从数据库中读取  材质
    private string Styles = "FJSX6";//风格
    private DataSet ds ;
    string result = "";
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        #region 
        Page.Title = BasePage.WebTitle;
        WebTitle = BasePage.WebTitle;
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            ZK = BasePage.GetValue(1);
        }
        else
        {
            ZK = "";
        }
        if (!IsPostBack)
        {
            if (Request.QueryString["PID"] != null)
            {
                ProductID = Request.QueryString["PID"].ToString();
                BasePage.UserView_Add(ProductID);
            }
            else
            {
                Response.Redirect("../Default.aspx");
            }
            if (Request.QueryString["Color"] != null)
            {
                Color = Request.QueryString["Color"].ToString();
            }
            else
            {
                Color = "";
            }
            BaseInit();
        }
        Session["Step"] = "0";
        if (Session["Transport"] != null)
        {
            Session["Transport"] = null;
        }
        if (Session["PayMethod"] != null)
        {
            Session["Transport"] = null;
        }
       
        #endregion
    }
    private void BaseInit()
    {
        string usercode = BasePage.GetValue(0);//此处为用户session信息

        string Fsign = "";//默认选中颜色
        ds = new DataSet();
        BLL.SHANGPIN shangpin = new BLL.SHANGPIN();
        if (Request.QueryString["PID"] != null)
        {
            if (Request.QueryString["Color"] != null)
            {
                SelectColor = Request.QueryString["Color"].ToString();
            }

            //用户在购物车中指定的商品数量
            Decimal result = 0.00M;
            StringBuilder cartSql = new StringBuilder(); //and BPD_COMMCOLOR='{1}' and BPD_COMMSIZE='{2}' and BPD_USERCODE='{3}'
            StringBuilder kcSql = new StringBuilder();//spdm='{0}' and GG1DM='{1}' and GG2DM='{2}' and ckdm='{3}' and kwdm='{4}'

            cartSql.Append(string.Format("select sum(isnull(BPD_QTY,0)) as QTY from BUYPOCKETDETAIL where BPD_COMMCODE='{0}' and BPD_USERCODE='{1}'", ProductID, usercode));
            kcSql.Append(string.Format(" spdm='{0}' and ckdm='{1}' and kwdm='{2}' ", ProductID, BasePage.GetCKDM(), BasePage.GetKWDM()));

            if (Request.QueryString["Color"] != null)
            {
                cartSql.Append(string.Format(" and BPD_COMMCOLOR='{0}'", SelectColor));
                kcSql.Append(string.Format(" and GG1DM='{0}' ", SelectColor));
            }

            Decimal qty = Decimal.TryParse(new BLL.BUYPOCKETDETAIL().GetSLByUser(cartSql.ToString()), out result) ? result : 0;

            Decimal kykcQTY = Decimal.TryParse(new BLL.SPKCB().OC_Product_KEYONG_Stocks(kcSql.ToString()), out result) ? result : 0.00M;
            Decimal sykc = 0.00M;
            if (kykcQTY == -1)
            { }
            else
            { sykc = kykcQTY - qty; }

            ds = shangpin.GetProductInfo(Request.QueryString["PID"]);
            if (ds.Tables.Count > 0)
            {
                DataTable dt0 = ds.Tables[0];//商品基本信息
                DataTable dt1 = ds.Tables[1];//商品颜色及对应商品全图


                DataTable dt2 = ds.Tables[2];//商品尺寸
                DataTable dt3 = ds.Tables[3];//商品局部图片列表


                foreach (DataRow dr in dt0.Rows) //添加商品基本信息
                {
                    ProductName = (dr["CA_NAME"] == DBNull.Value ? dr["SPMC"].ToString() : dr["CA_NAME"].ToString());
                    Fabrics = (dr[CaiZhi] == DBNull.Value ? "" : dr[CaiZhi].ToString());
                    Price = (dr[DPJ] == DBNull.Value ? "0.00" : double.Parse(dr[DPJ].ToString()).ToString("f"));
                    Brand = (dr["PinPai"] == DBNull.Value ? "" : dr["PinPai"].ToString());
                    Note = (dr["CA_NOTE"] == DBNull.Value ? "" : BasePage.Getstr(dr["CA_NOTE"].ToString()));
                    Weight = (dr["CA_WEIGHT"] == DBNull.Value ? "0.00" : string.Format("{0:F0}", dr["CA_WEIGHT"]));
                    Style = (dr[Styles] == DBNull.Value ? "" : dr[Styles].ToString());                    
                    if (SelectColor == null)
                    {
                       SelectColor=(dr["Fsign"] == DBNull.Value ? "" : dr["Fsign"].ToString());//默认颜色
                    } 
                }
                Colorlist = GetColorListHtml(dt1, SelectColor);  //颜色列表
                Sizelist = GetSizeListHtml(dt2);                //尺寸列表
                Partimglist = GetPartImgListHtml(dt3,SelectColor);          //加载局部图
                Bigsrc = GetInitBigPic(SelectColor, dt1);       //大图路径
                Stock = sykc.ToString();
                if (Convert.ToDouble ( Stock) <= 0)
                {
                    kc = "库存不足";
                }
            }
            string coloroName = new BLL.GUIGE1().GetMC(SelectColor);
            Page.Title = ProductName + " " + coloroName + " " + BasePage.WebTitle;
        }
        else
        {
 
        }
    }
    #region 返回颜色列表
    private string GetColorListHtml(DataTable dt_color,string selectedcolor)
    {
        StringBuilder colorhtml_sb = new StringBuilder();
        colorhtml_sb.Append("<table width=390px border=0 cellspacing=0 cellpadding=0><tr>");
        colorhtml_sb.Append("<td width=40px>颜色：</td>");
        colorhtml_sb.Append("<td>");

        foreach (DataRow dr in dt_color.Rows)
        {
            colorhtml_sb.Append("<table id='colors' class=color  border=0 cellspacing=0 cellpadding=0>");
            colorhtml_sb.Append("<tr>");
            colorhtml_sb.Append("<td class=rb_yanse_td>");
            colorhtml_sb.Append(dr["GGMC"].ToString());
            colorhtml_sb.Append("</td>");
            colorhtml_sb.Append("</tr>");
            colorhtml_sb.Append("<tr>");
            colorhtml_sb.Append("<td height=22 align=center valign=middle>");
            if (selectedcolor == dr["CP_COLOR"].ToString())    //加默认选中样式
            {
                hdColorCode.Value = dr["CP_COLOR"].ToString();
                colorhtml_sb.Append("<a href='###' onclick=setProduct('" + dr["CP_CODE"].ToString() + "','" + dr["CP_COLOR"].ToString() + "') >");
                colorhtml_sb.Append("<img id='Color" + dr["CP_COLOR"].ToString() + "' src='" + BLL.Common.GetColorPath() + (dr["CA_PATH"] == DBNull.Value ? "" : dr["CA_PATH"].ToString()) + "' class=rb_imglj />");
                colorhtml_sb.Append("</a>");
            }
            else
            {
                colorhtml_sb.Append("<a href='###' onclick=setProduct('" +dr["CP_CODE"].ToString()+"','"+ dr["CP_COLOR"].ToString() + "') >");
                colorhtml_sb.Append("<img id='Color" + dr["CP_COLOR"].ToString() + "' src='" + BLL.Common.GetColorPath() + (dr["CA_PATH"] == DBNull.Value ? "" : dr["CA_PATH"].ToString()) + "' />");
                colorhtml_sb.Append("</a>");
            }
            colorhtml_sb.Append("</td>");
            colorhtml_sb.Append("</tr>");
            colorhtml_sb.Append("</table>");
        }
        colorhtml_sb.Append("</td></tr></table>");
        return colorhtml_sb.ToString();
    }
    #endregion

    #region 返回尺寸列表
    private string GetSizeListHtml(DataTable dt_Size)
    {
        StringBuilder Sizehtml_sb = new StringBuilder();
        Sizehtml_sb.Append("<select name=\"select2\" id=\"sizeList\" onchange=\"ChangeKC('" + ProductID + "','" + SelectColor + "',this.value)\">");
        Sizehtml_sb.Append("<option value=\"-1\">请选择尺码</option>");
        foreach (DataRow dr in dt_Size.Rows)
        {
            Sizehtml_sb.Append("<option value=\"" + dr["GGDM"].ToString() + "\">" + dr["GGMC"].ToString() + "</option>");
        }
        Sizehtml_sb.Append("</select>");
        return Sizehtml_sb.ToString();
    }
    #endregion

    #region 返回局部图列表
    private string GetPartImgListHtml(DataTable partimg,string value)
    {
        StringBuilder sb_partimg = new StringBuilder();
        int j = 0;
        for (int i = 0; i < partimg.Rows.Count; i++)
        {
            if (value == partimg.Rows[i]["CP_Color"].ToString())
            {
                if (j == 0)
                {
                    sb_partimg.Append("<li id='Part" + GetPname(partimg.Rows[i]["CP_PICPATH"].ToString()) + "' class=\"rb_imglj\"><a href='###' onclick=setBigImg('" + GetPname(partimg.Rows[i]["CP_PICPATH"].ToString()) + "')><img src='" + BLL.Common.GetProductPicPath("small") + BLL.Common.GetPath(partimg.Rows[i]["CP_PICPATH"].ToString()) + partimg.Rows[i]["CP_PICPATH"].ToString() + "' /></a></li>");
                    j++;
                }
                else
                {
                    sb_partimg.Append("<li id='Part" + GetPname(partimg.Rows[i]["CP_PICPATH"].ToString()) + "'><a href='###' onclick=setBigImg('" + GetPname(partimg.Rows[i]["CP_PICPATH"].ToString()) + "')><img src='" + BLL.Common.GetProductPicPath("small") + BLL.Common.GetPath(partimg.Rows[i]["CP_PICPATH"].ToString()) + partimg.Rows[i]["CP_PICPATH"].ToString() + "' /></a></li>");
                }
            }
        }
        return sb_partimg.ToString();
    }
    #region 返回局部图片名称不包含后坠
    private string GetPname(string pname)
    {
        string result = "";
        if (pname.Length > 0)
        {
            int fir = pname.LastIndexOf('_');
            int index = pname.IndexOf('.');
            if (index > 0)
            {
                result = pname.Substring(fir + 1, index - fir - 1);
            }
        }
        return result;
    }
    #endregion
    #endregion

    #region 返回初始化大图

    private string GetInitBigPic(string fsign, DataTable dt)
    {
        string result = "";        
        foreach (DataRow dr in dt.Rows)
        {
            if (dr["CP_COLOR"].ToString() == fsign)
            {
                result = BLL.Common.GetProductPicPath("big") + BLL.Common.GetPath(dr["CP_PICPATH"].ToString()) + dr["CP_PICPATH"].ToString();
            }
        }
        return result;
    }
    #endregion
    #region ICallbackEventHandler 成员
    
    string ICallbackEventHandler.GetCallbackResult()
    {
        return result;
    }

    void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
    {

        result = new BLL.GUIGE1().GetMC(eventArgument);
    }

    #endregion
}
